package com.zliblike_platform.controller;

import com.zliblike_platform.entity.Category;
import com.zliblike_platform.service.CategoryService;
import com.zliblike_platform.vo.CategoryVO;
import com.zliblike_platform.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 图书分类控制器
 */
@RestController
@RequestMapping("/api/categories")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 获取所有分类
     * @return 分类列表
     */
    @GetMapping
    public ResponseResult<List<CategoryVO>> getAllCategories() {
        return ResponseResult.success(categoryService.getAllCategories());
    }

    /**
     * 获取分类树
     * @return 分类树结构
     */
    @GetMapping("/tree")
    public ResponseResult<List<CategoryVO>> getCategoryTree() {
        return ResponseResult.success(categoryService.getCategoryTree());
    }

    /**
     * 根据ID获取分类
     * @param categoryId 分类ID
     * @return 分类信息
     */
    @GetMapping("/{categoryId}")
    public ResponseResult<CategoryVO> getCategoryById(@PathVariable Long categoryId) {
        return ResponseResult.success(categoryService.getCategoryById(categoryId));
    }

    /**
     * 添加分类
     * @param category 分类信息
     * @return 添加结果
     */
    @PostMapping
    public ResponseResult<Void> addCategory(@RequestBody Category category) {
        categoryService.addCategory(category);
        return ResponseResult.success();
    }

    /**
     * 更新分类
     * @param categoryId 分类ID
     * @param category 分类信息
     * @return 更新结果
     */
    @PutMapping("/{categoryId}")
    public ResponseResult<Void> updateCategory(@PathVariable Long categoryId, @RequestBody Category category) {
        category.setCategoryId(categoryId);
        categoryService.updateCategory(category);
        return ResponseResult.success();
    }

    /**
     * 删除分类
     * @param categoryId 分类ID
     * @return 删除结果
     */
    @DeleteMapping("/{categoryId}")
    public ResponseResult<Void> deleteCategory(@PathVariable Long categoryId) {
        categoryService.deleteCategory(categoryId);
        return ResponseResult.success();
    }
} 